package com.wxq.workpad.lailiao

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.samluys.statusbar.StatusBarUtils
import com.wxq.workpad.R
import com.wxq.workpad.base.BaseActivity
import com.wxq.workpad.base.RealShow
import com.wxq.workpad.databinding.ActivityLaiLiaoBinding
import com.wxq.workpad.entity.SaiXuanItem
import com.wxq.workpad.lailiao.adapter.SaiXuanListAdapter
import com.wxq.workpad.lailiao.dialog.ChooseSaiXuanDialog
import com.wxq.workpad.lailiao.dialog.ChooseSaiXuanDialogOld

class LaiLiaoActivity : BaseActivity<ActivityLaiLiaoBinding>() {


    private lateinit var fragments: MutableList<Fragment>
    private lateinit var tabTitles: MutableList<String>

    lateinit var chooseSaiXuanDialogOld: ChooseSaiXuanDialog


    // 提供给recycleview 的数据
    private var saiXuanListWithInput: MutableList<SaiXuanItem>


    init {
//        saiXuanDialogList = ArrayList()
        saiXuanListWithInput = ArrayList()

        saiXuanListWithInput.add(SaiXuanItem("到货单号", 1))
        saiXuanListWithInput.add(SaiXuanItem("供方", 1))
        saiXuanListWithInput.add(SaiXuanItem("编码", 1))
        saiXuanListWithInput.add(SaiXuanItem("名称", 1))
        saiXuanListWithInput.add(SaiXuanItem("厂家", 1))
        saiXuanListWithInput.add(SaiXuanItem("图号", 1))
        saiXuanListWithInput.add(SaiXuanItem("类型", 2,"采购到货","委外到货"))
        saiXuanListWithInput.add(SaiXuanItem("来源", 2,"ERP","QMS"))
        saiXuanListWithInput.add(SaiXuanItem("日期", 3))
    }


    override fun initData() {

    }

    override fun initView(instanceState: Bundle?) {
        StatusBarUtils.transparencyBar(this@LaiLiaoActivity)
        StatusBarUtils.StatusBarIconDark(this@LaiLiaoActivity)
        tabTitles = ArrayList()
        tabTitles.add("全部")
        tabTitles.add("待检验")
        tabTitles.add("已检验")
        fragments = ArrayList()
        fragments.add(LaiLiaoFragment.newInstance(""))
        fragments.add(LaiLiaoFragment.newInstance("1"))
        fragments.add(LaiLiaoFragment.newInstance("2"))
        binding.viewpager.offscreenPageLimit = 3
        binding.viewpager.adapter =
            LaiLiaoFragmentAdapter(supportFragmentManager, fragments, tabTitles)
        binding.viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                if (fragments.get(position) is RealShow) {
                    (fragments.get(position) as RealShow).realShow()
                }
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
        binding.tabLayout.setTabMode(TabLayout.MODE_FIXED)
        binding.tabLayout.setupWithViewPager(binding.viewpager)
        binding.tabLayout.setTabsFromPagerAdapter(binding.viewpager.adapter)


        binding.viewpager.setCurrentItem(0)
        if (fragments.get(0) is RealShow) {
            (fragments.get(0) as RealShow).realShow()
        }
        binding.ivBack.setOnClickListener {
            onBackPressed()
        }

        binding.ivSaixuan.setOnClickListener {

            chooseSaiXuanDialogOld.showDialog(saiXuanListWithInput)
        }
        chooseSaiXuanDialogOld =
            ChooseSaiXuanDialog(mContext)
        chooseSaiXuanDialogOld.setListener(object : ChooseSaiXuanDialog.ClickItemListener {
            override fun click(position: Int) {

            }

            override fun getConfirmData(data: MutableList<SaiXuanItem>?) {
                 // 开始查询
                if (data!=null) {
                    var hasValue=false
                    data.forEach {
                        if (!it.value.equals("")) {
                            hasValue=true
                        }
                    }
                    if (hasValue) {
                        binding.ivSaixuan.setImageResource(R.mipmap.hassaixuan)
                    }else{
                        binding.ivSaixuan.setImageResource(R.mipmap.saixuan)
                    }
                }


            }
        })


    }

    companion object {

        @JvmStatic
        fun newInstance(context: Context) {
            val intent = Intent(context, LaiLiaoActivity::class.java)
            context.startActivity(intent)
        }
    }


}